
/**
 * @class Kohive.FormPanel
 * @extends Ext.form.FormPanel
 * Customizes the Ext.form.FormPanel with a new template
 */
Kohive.FormPanel = function(config) {
	var config = config || {};

	this.addEvents(
			/**
			 * @event create
			 * Fires when the 'Add' button is clicked indicating the user wants to save the new Bookmark
			 * @param {Ext.form.FormPanel} this The form panel
			 */
			'create'
	);

	Ext.applyIf(config, {
				// hidden: true,
				cls			: 'x-kohive-form',
				labelWidth	: 60,
				labelAlign	: 'right',
				defaults	: {
					anchor	: '100%'
				},
				buttonAlign	: 'right'
			});

	Kohive.FormPanel.superclass.constructor.call(this, config);

	this.on('create', this.beforeSubmit, this);
};
Ext.extend(Kohive.FormPanel, Ext.form.FormPanel, {
			// private
			onCollapse		: function(doAnim, animArg) {
				if (doAnim) {
					this.el.slideOut(this.slideAnchor, Ext.apply(this
											.createEffect(animArg || true,
													this.afterCollapse, this
											), this.collapseDefaults));
				} else {
					this.el.hide();
					this.afterCollapse();
				};
			},

			// private
			onExpand		: function(doAnim, animArg) {
				if (doAnim) {
					this.el.slideIn(this.slideAnchor, Ext.apply(this
											.createEffect(animArg || true,
													this.afterExpand, this
											), this.expandDefaults));
				} else {
					this.el.show();
					this.afterExpand();
				};
			},

			/**
			 * Delegates the form/control errors
			 * @param {Object} errors An object will errors inside it
			 * @return {String} Return value description
			 */
			delegateErrors	: function(errors) {
				// console.log('delegate');
				if (!errors) {
					return;
				};
			},

			/**
			 * Highlights the field with correct styling when it has an error
			 */
			showFieldError	: function(error) {
				var nId = error.name;
				var k = error.key;

				if (k == 'validation.required') {
					var n = Ext.get(nId);

					if (n) {
						n.addClass('x-form-invalid');
					};
				};
			},

			/**
			 * Checks if all the form items are valid
			 * @return {Boolean} Returns true/false
			 */
			isValid			: function() {
				var valid = true;

				// Loop through each of the form items and check if they are valid
				this.items.each(function(f) {
							f.validate();
							if (f.isValid && !f.isValid(true)) {
								valid = false;
							}
						});

				// If anything is invalid, return false
				if (!valid) {
					this.afterSubmit();
					return false;
				};

				return true;
			},

			/**
			 * Is called before anything is submited
			 */
			beforeSubmit	: function() {
				this.btn.disable();
			},

			/**
			 * Is called after the form has been subitted
			 */
			afterSubmit		: function() {
				this.btn.enable();
			},

			/**
			 * Function description
			 */
			getAddButton	: function() {
				/**
				 * Kohive button which is used in the form
				 */
				this.btn = new Kohive.SmallButton({
							text	: 'Add',
							scope	: this,
							handler	: this.fireEvent.createDelegate(this, [
											'create', this])
						});

				return this.btn;
			}
		});

Ext.reg('kohive_form_panel', Kohive.FormPanel);